package com.wsz.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.eduservice.listener.SubjectExcelListener;
import com.wsz.eduservice.pojo.Subject;
import com.wsz.eduservice.pojo.excel.SubjectData;
import com.wsz.eduservice.pojo.subject.OneSubject;
import com.wsz.eduservice.pojo.subject.TwoSubject;
import com.wsz.eduservice.service.SubjectService;
import com.wsz.eduservice.mapper.SubjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
* @author wangs
* @description 针对表【edu_subject(课程科目)】的数据库操作Service实现
* @createDate 2022-09-23 18:45:21
*/
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject>
    implements SubjectService{

    @Override
    public void saveSubject(MultipartFile file,SubjectService subjectService) {
        try{
            EasyExcel.read(file.getInputStream(), SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
        }
    }

    @Override
    public List<OneSubject> getAllOneSubject() {

        //一级目录
        QueryWrapper<Subject> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id","0");
        List<Subject> oneSubject = baseMapper.selectList(queryWrapper);


        //二级目录
        QueryWrapper<Subject> queryWrapper1 = new QueryWrapper();
        queryWrapper1.ne("parent_id", "0");
        List<Subject> twoSubject = baseMapper.selectList(queryWrapper1);

        List<OneSubject> finalSubject = new ArrayList<>();

        for (int i = 0; i < oneSubject.size(); i++) {
            Subject subject = oneSubject.get(i);
            OneSubject oneSubject1 = new OneSubject();
            BeanUtils.copyProperties(subject,oneSubject1);

            List<TwoSubject> twoSubjectList = new ArrayList<>();

            for (int j = 0; j < twoSubject.size(); j++) {
                Subject subject1 = twoSubject.get(j);
                if (subject.getId().equals(subject1.getParentId())){
                    TwoSubject twoSubject1 = new TwoSubject();
                    BeanUtils.copyProperties(subject1,twoSubject1);
                    twoSubjectList.add(twoSubject1);
                }
            }

            oneSubject1.setChildren(twoSubjectList);

            finalSubject.add(oneSubject1);

        }

        return finalSubject;
    }
}




